package leetcode.interview;

import leetcode.base.ListNode;

/**
 * ClassName: $0205_addTwoNumbers
 * Description:https://leetcode-cn.com/problems/sum-lists-lcci/ 链表求和
 * date: 2022-02-19 19:16
 *
 * @author liyifan
 */
public class $0205_addTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) return l2; if (l2 == null) return l1;
        ListNode dummy = new ListNode(0);
        int x = 0;  // 进位
        ListNode p = dummy;
        while (l1 != null || l2 != null || x != 0) {
            int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + x;
            x = sum / 10;
            p = new ListNode(sum % 10);
            p = p.next;
            l1 = (l1 == null) ? null : l1.next;
            l2 = (l2 == null) ? null : l2.next;
        }
        return dummy.next;
    }
}
